package com.sgcc.sx.pmos.common.utils;


import android.app.Dialog;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;


import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.sgcc.pmos.nativesdk.R;

import java.util.Calendar;

public class TimePickerUtil {
    private int itemsVisibleCount = 7;
    private int textColorConfirm = -16417281;
    private int textColorCancel = -16417281;
    private boolean[] type = new boolean[]{true, true, true, false, false, false};
    private OnTimeSelectListener onTimeSelectListener;
    private String selectime;
    private String format;
    private Calendar mStartDate, mEndDate;
    private String mTitleText;
    private boolean isCyclic = false;

    public TimePickerUtil setCyclic(boolean cyclic) {
        isCyclic = cyclic;
        return this;
    }

    public TimePickerUtil setSelectime(String selectime) {
        this.selectime = selectime;
        return this;
    }

    public TimePickerUtil setRangDate(Calendar startDate, Calendar endDate) {
        mStartDate = startDate;
        mEndDate = endDate;
        return this;
    }

    public TimePickerUtil setTitleText(String titleText) {
        mTitleText = titleText;
        return this;
    }

    public TimePickerUtil setFormat(String format) {
        this.format = format;
        return this;
    }

    public synchronized static TimePickerUtil getInstance() {
        return new TimePickerUtil();
    }


    public TimePickerUtil setType(boolean[] type) {
        this.type = type;
        return this;
    }

    public TimePickerUtil setOnTimeSelectListener(OnTimeSelectListener onTimeSelectListener) {
        this.onTimeSelectListener = onTimeSelectListener;
        return this;
    }

    public TimePickerUtil setItemsVisibleCount(int itemsVisibleCount) {
        this.itemsVisibleCount = itemsVisibleCount;
        return this;
    }


    public TimePickerUtil setTextColorConfirm(int textColorConfirm) {
        this.textColorConfirm = textColorConfirm;
        return this;
    }


    public TimePickerUtil setTextColorCancel(int textColorCancel) {
        this.textColorCancel = textColorCancel;
        return this;
    }

    public void build() {
        TimePickerView pvTime1 = new TimePickerBuilder(ActivityUtils.getTopActivity(), onTimeSelectListener)
                .setType(type)
                .setItemVisibleCount(itemsVisibleCount) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setSubmitText("确认")
                .setCancelText("取消")
                .setSubmitColor(textColorConfirm < 0 ? ActivityUtils.getTopActivity().getResources().getColor(R.color._09B1FF) : textColorConfirm)//确定按钮文字颜色
                .setCancelColor(textColorCancel < 0 ? ActivityUtils.getTopActivity().getResources().getColor(R.color._555555) : textColorCancel)//取消按钮文字颜色
                .setLineSpacingMultiplier(2.0f)
                .setOutSideCancelable(true)
                .setTitleText(mTitleText)
                .setDate(getDate())
                .isCyclic(isCyclic)
                .setRangDate(getStartDate(), getEndDate())
                .isAlphaGradient(true)
                .build();
        show(pvTime1);
    }

    public Calendar getEndDate() {
        Calendar calendar = Calendar.getInstance();
        if (mEndDate == null) {
            calendar.set(calendar.get(Calendar.YEAR) + 100, 12, 31);
            return calendar;
        } else {
            return mEndDate;
        }
    }

    public Calendar getStartDate() {
        Calendar calendar = Calendar.getInstance();
        if (mStartDate == null) {
            calendar.set(1900, 1, 1);
            return calendar;
        } else {
            return mStartDate;
        }
    }

    private Calendar getDate() {
        try {
            if (!TextUtils.isEmpty(selectime)) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(TimeUtils.string2Date(selectime, format));
                return calendar;
            } else {
                return Calendar.getInstance();
            }
        } catch (Exception e) {
            return Calendar.getInstance();
        }

    }

    /**
     * @author yinsh
     * @description 显示时间选择器
     * @date 2021/7/9 上午9:55
     */
    private void show(TimePickerView pvTime1) {
        Dialog mDialog = pvTime1.getDialog();
        if (mDialog != null) {
            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
                dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
                layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    // 延伸显示区域到刘海
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                    dialogWindow.setAttributes(lp);
                    // 设置页面全屏显示
                    final View decorView = dialogWindow.getDecorView();
                    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                }
                dialogWindow.setAttributes(layoutParams);
            }
        }
        pvTime1.show();
    }
}
